<?php
import('plugins.reports.articles.ArticleReportDAO');
import('classes.journal.JournalDAO');
import('classes.journal.SectionDAO');
import('classes.submission.sectionEditor.SectionEditorSubmissionDAO');
import('lib.pkp.classes.session.SessionManager');
import('classes.submission.editor.EditorSubmissionDAO');
import('classes.submission.sectionEditor.SectionEditorSubmissionDAO');
import('classes.journal.SectionDAO');


function connBD(){
	$conn = mysql_connect("localhost","ojs","ojs", true);

	if(!$conn){
		die("Impossible conectar a BD" . mysql_error());
	}
	$bd = mysql_select_db("ojs", $conn);

	if(!$bd){
		mysql_connect($conn);
		die("Error en BD" . mysql_error());
	}

	return $conn;
}

function smarty_function_estadisticas(array $params, $Smarty_Template_Instance) {
    
    //Se crea un objeto de tipo JournalDAO
	$journalDAO = new JournalDAO();
	DAORegistry::registerDAO('JournalDAO', $journalDAO);
	$journalDAO =& DAORegistry::getDAO('JournalDAO');

	/* 
	Se consultan todas las revistas activadas
	*/
	$revistasActivasDAORF = $journalDAO->getEnabledJournals();

	/*
	Como la consulta anterior retorna un array de tipo DAOResultFactory, este objeto tiene un meotod para pasarlos a array de tipo Journal
	*/
	$revista = $revistasActivasDAORF->toArray();

	$idJournal = 0;
	/*
	Una vez que tenemos un array de tipo Journal, lo que hacemos es consultar la id del primer elemento (ya que no habrá más revistas, si las hubiese habría que cambiar esto)
	*/
	$idJournal = (int) $revista[0]->getJournalId();

	if($idJournal > 0){	
		// Articulos sin asignacion
		$Unassignement = getStatusArticlesUnassignement($idJournal);
		//var_dump($Unassignement);
		//$charUnassignement = construirVectorGrafica($Unassignement);

		// Articulos en revision
		$InReview = getStatusArticlesInReview($idJournal);
		$InReviewJson = json_encode($InReview, JSON_UNESCAPED_UNICODE);
		//$charInReviewt = construirVectorGrafica($InReview);

		// Articulos en edicion
		$InEditing = getStatusArticlesInEditing($idJournal);
		//var_dump($InEditing);
		//$charInEditing = construirVectorGrafica($InEditing);

		
		exit;
	}else{
		return "Error al obtener el identificador de la revista";
	}
}


function construirVectorGrafica($articulos){
	

}


function getStatusArticlesUnassignement($idJournal){

	$conn = connBD();
	$q = mysql_query('SELECT ss.setting_value AS section, COUNT(*) AS count
			FROM articles a
				LEFT JOIN edit_assignments e ON (a.article_id = e.article_id)
				LEFT JOIN edit_assignments e2 ON (a.article_id = e2.article_id AND e.edit_id < e2.edit_id)
				JOIN section_settings ss ON (a.section_id = ss.section_id AND ss.setting_name = "title")

			WHERE	a.journal_id = '.$idJournal.'
				AND a.submission_progress = 0
				AND a.status = ' . STATUS_QUEUED . '
				AND e2.edit_id IS NULL
				AND e.edit_id IS NULL
			GROUP BY a.section_id',$conn);

	if(!$q){
		die("ERROR" . mysql_error());
	}

	$i = 0;
	$articles = array();
	while($row = mysql_fetch_array($q)){
		$section_name = utf8_encode(html_entity_decode($row['section']));
		$articles[$i]= array($section_name => $row['count']);

		$i++;
	}

return $articles;
}

function getStatusArticlesInReview($idJournal){

	$conn = connBD();
	$q = mysql_query('SELECT ss.setting_value AS section, COUNT(*) AS count
			FROM articles a
				LEFT JOIN edit_assignments e ON (a.article_id = e.article_id)
				LEFT JOIN edit_assignments e2 ON (a.article_id = e2.article_id AND e.edit_id < e2.edit_id)
				LEFT JOIN edit_decisions d ON (a.article_id = d.article_id)
				LEFT JOIN edit_decisions d2 ON (a.article_id = d2.article_id AND d.edit_decision_id < d2.edit_decision_id)	
				JOIN section_settings ss ON (a.section_id = ss.section_id AND ss.setting_name = "title")

			WHERE a.journal_id = '.$idJournal.'
				AND a.submission_progress = 0
				AND a.status = ' . STATUS_QUEUED . '
				AND e2.edit_id IS NULL
				AND e.edit_id IS NOT NULL
				AND d2.edit_decision_id IS NULL
				AND (d.decision IS NULL OR d.decision <> ' . SUBMISSION_EDITOR_DECISION_ACCEPT . ')
			GROUP BY a.section_id',$conn);

	if(!$q){
		die("ERROR" . mysql_error());
	}

	$i = 0;
	$articles = array();
	while($row = mysql_fetch_array($q)){

		$section_name = utf8_encode(html_entity_decode($row['section']));
		$articles[$i]= array($section_name => $row['count']);

		$i++;
	}

	$InReviewJson = json_encode($articles, JSON_UNESCAPED_UNICODE);
	return $InReviewJson;

}


function getStatusArticlesInEditing($idJournal){

	$conn = connBD();
	$q = mysql_query('SELECT ss.setting_value AS section, COUNT(*) AS count
			FROM articles a
				LEFT JOIN edit_assignments e ON (a.article_id = e.article_id)
				LEFT JOIN edit_assignments e2 ON (a.article_id = e2.article_id AND e.edit_id < e2.edit_id)
				LEFT JOIN edit_decisions d ON (a.article_id = d.article_id)
				LEFT JOIN edit_decisions d2 ON (a.article_id = d2.article_id AND d.edit_decision_id < d2.edit_decision_id)
				JOIN section_settings ss ON (a.section_id = ss.section_id AND ss.setting_name = "title")

			WHERE	a.journal_id = '.$idJournal.'
				AND a.submission_progress = 0
				AND a.status = ' . STATUS_QUEUED . '
				AND e2.edit_id IS NULL
				AND e.edit_id IS NOT NULL
				AND d2.edit_decision_id IS NULL
				AND d.decision = ' . SUBMISSION_EDITOR_DECISION_ACCEPT . '
				GROUP BY a.section_id',$conn);

	if(!$q){
		die("ERROR" . mysql_error());
	}

	$i = 0;
	$articles = array();
	while($row = mysql_fetch_array($q)){
		$section_name = utf8_encode(html_entity_decode($row['section']));
		$articles[$i]= array($section_name => $row['count']);

		$i++;
	}

	$InEditingJson = json_encode($articles, JSON_UNESCAPED_UNICODE);

	return $InEditingJson;

}

?>